﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _059.LINQ
{
	internal class Program
	{
		static void Main(string[] args)
		{
			// 武林高手
			var masterList = new List<MartialArtsMaster>()
			{
				new MartialArtsMaster() {Id = 100,Name="张三",Age = 100,Kongfu = "葵花宝典",Level=9},
				new MartialArtsMaster() {Id = 200,Name="张三2",Age = 100,Kongfu = "葵花宝典2",Level=9},
				new MartialArtsMaster() {Id = 300,Name="张三3",Age = 100,Kongfu = "打狗棒法",Level=9},
				new MartialArtsMaster() {Id = 400,Name="张三4",Age = 100,Kongfu = "葵花宝典4",Level=9},
				new MartialArtsMaster() {Id = 500,Name="张三5",Age = 100,Kongfu = "打狗棒法",Level=9}
			};

			// 武学
			var kongfuList = new List<Kongfu>
			{
				new Kongfu() {Id=1,Name="打狗棒法",Power=90},
				new Kongfu() {Id=2,Name="打狗棒法1",Power=95},
				new Kongfu() {Id=3,Name="打狗棒法2",Power=100},
				new Kongfu() {Id=4,Name="打狗棒法3",Power=80},
			};

			// 查询所有级别大于8的
			//var res = new List<MartialArtsMaster>();
			//foreach (var temp in masterList)
			//{
			//	if (temp.Level > 8)
			//	{
			//		res.Add(temp);
			//	}
			//}

			// 使用LINQ查询 （表达式写法）
			//var res = from m in masterList where m.Level > 8 && m.Age == 100 select m;

			// 委托类型
			//var res =  masterList.Where(Test1);
			// lamda
			var res = masterList.Where(m => m.Level > 8 && m.Age == 100);
			foreach (var item in res)
			{
				Console.WriteLine(item);
			}

			Console.WriteLine("--------------------");

			// 联合查询
			//var res1 = from m in masterList
			//		   from k in kongfuList
			//		   where m.Kongfu == k.Name && k.Power >=90
			//		   select new {master = m,kongfu = k};
			// 扩展方法
			var res1 = masterList.SelectMany(m => kongfuList, (m, k) => new { master = m, kongfu = k })
				.Where(x => x.master.Kongfu == x.kongfu.Name && x.kongfu.Power >= 90);
			foreach (var item in res1)
			{
				Console.WriteLine(item);
			}
			Console.WriteLine("--------------------");

			// 排序
			//var res2 = from m in masterList 
			//		   where m.Level > 8 && m.Age == 100
			//		   orderby m.Id descending
			//		   select m;
			var res2 = masterList.Where(m => m.Level > 8 && m.Age == 100)
				.OrderByDescending(m => m.Id).ThenBy(m => m.Age);// ThenBy ID相同再根据年龄排序
			foreach (var item in res2)
			{
				Console.WriteLine(item);
			}

			Console.WriteLine("--------------");

			// 集合联合查询 join on
			var res3 = from m in masterList
					   join k in kongfuList
					   on m.Kongfu equals k.Name
					   where k.Power >= 90
					   select new { master = m, kongfu = k };
			foreach (var item in res3)
			{
				Console.WriteLine(item);
			}

			Console.WriteLine("--------------");

			// 分组查询 into groups
			var res4 = from k in kongfuList
					   join m in masterList
					   on k.Name equals m.Kongfu
					   into groups
					   orderby groups.Count() descending
					   select new {kongfu = k,count = groups.Count() };
			foreach (var item in res4)
			{
				Console.WriteLine(item);
			}

			Console.WriteLine("--------------");

			// 按照自身字段进行分组 group by
			var res5 = from m in masterList
					   group m by m.Kongfu into g
					   select new {count=g.Count(),g.Key};// key表示按照那个属性分的组
			foreach (var item in res5)
			{
				Console.WriteLine(item);
			}
			Console.WriteLine("--------------");

			// 量词操作符 
			// any 是否有一个有
			bool r = masterList.Any(m => m.Menpai == "丐版");
            Console.WriteLine(r);
			// 是否都属于 all
			masterList.All(m => m.Menpai == "丐版");
			Console.WriteLine(r);
		}

		// 过滤方法
		static bool Test1(MartialArtsMaster master)
		{
			if (master.Level > 8)
			{
				return true;
			}
			return false;
		}
	}
}
